package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.dbmodel.LogindbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by GordonLjx on 2016/9/25.
 */
public class LogindbService {
    //插入或者更新数据
    public static boolean saveOrUpdateLogindbMode(LogindbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            //System.out.print("保存的昵称："+model.getNickname()+"\n");
            long expires = Long.valueOf(model.getExpires())*1000;
            expires += System.currentTimeMillis();
            model.setExpires(expires+"");
            db.saveOrUpdate(model);
            CustomApplication.loginModel.setExpires(model.getExpires());
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @return
     */
    //查找数据
    public static LogindbModel getLogindbModel(){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(LogindbModel.class) != null){
                List<LogindbModel> list = db.selector(LogindbModel.class).findAll();
                if (list == null || list.size() == 0){
                    return null;
                }
                return list.get(0);
            }
            return null;
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    //删除数据
    public static boolean deleteLoginModel(){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(LogindbModel.class);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }
}
